﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class works : System.Web.UI.Page
    {
        private int Nowpage = 0;
        private static int CategoryID = 0;
        private const int PAGESIZE = 20;
        private int pagecount = 0;
        

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "案例";
            
            try
            {
                Nowpage = Int32.Parse(Request.QueryString["pid"].ToString());
            }
            catch
            {
                Nowpage = 1;   
            }
            try
            {
                CategoryID = Int32.Parse(Request.QueryString["wid"].ToString());
                
            }
            catch
            {
                CategoryID = 0;
            }
            Session["WCategoryID"] = CategoryID;
            try
            {
                pagecount = WorksInfoLogic.GetPageCount(PAGESIZE, CategoryID);
            }
            catch
            {
                pagecount = 0;
            }
            

            if (!IsPostBack)
            {
                UpdateParamInfoLogic.UpdateAllWorksAbout();
                WorksCategoryBindData(CategoryID);
                WorksBindData(CategoryID,Nowpage);
                PageIndexBindData(pagecount, Nowpage,CategoryID);
            }
            try
            {
                Nowpage = Int32.Parse(lbl_NowPage.Text.Trim());
            }
            catch
            {
                Nowpage = 1;
            }
        }

        public void WorksCategoryBindData(int CategoryID)
        {
            List<WorksCategoryInfo> categorylist = WorksCategoryInfoLogic.GetAllWorksCategoryInfo(CategoryID);

            if (categorylist == null)
            {
                categorylist = new List<WorksCategoryInfo>();
                categorylist.Add(new WorksCategoryInfo());
            }

            WorkCategoryRepeater.DataSource = categorylist;
            WorkCategoryRepeater.DataBind();
        }

        public void WorksBindData(int CategoryID, int nowpage)
        {
            List<WorksInfo> workList = new List<WorksInfo>();
            if (CategoryID == 0)
            {
                workList = WorksInfoLogic.GetAllWorksInfo(PAGESIZE,Nowpage);
            }
            else
            {
                workList = WorksInfoLogic.GetCategoryWorksInfo(CategoryID, PAGESIZE, nowpage);
            }

            if (workList == null)
            {
                workList = new List<WorksInfo>();
                workList.Add(new WorksInfo());
            }
            WorksList.DataSource = workList;
            WorksList.DataBind();
            if (pagecount > 1)
            {
                if (nowpage == 1)
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = true;
                }
                else if (nowpage == pagecount)
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = false;
                }
                else
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = true;
                }
                btn_pre.Visible = true;
                btn_next.Visible = true;
            }
            else
            {
                btn_pre.Visible = false;
                btn_next.Visible = false;
            }
        }

        public void PageIndexBindData(int Pagecount, int NowPage,int CategoryID)
        {
            List<PageIndexInfo> indexlist = PageIndexLogic.GetPageIndexInfo(Pagecount, NowPage,CategoryID);
            if (indexlist == null)
            {
                indexlist = new List<PageIndexInfo>();
                indexlist.Add(new PageIndexInfo());
            }
            PageIndexReperter.DataSource = indexlist;
            PageIndexReperter.DataBind();
            if (Pagecount < 2)
            {
                PageIndexReperter.Visible = false;
            }
            else
            {
                PageIndexReperter.Visible = true;
            }
        }

        protected void btn_pre_Click(object sender, ImageClickEventArgs e)
        {
            Nowpage--;
            if (Nowpage >= 1)
            {
                lbl_NowPage.Text = Nowpage.ToString();
                WorksBindData(CategoryID, Nowpage);
                PageIndexBindData(pagecount, Nowpage, CategoryID);
            }
        }

        protected void btn_next_Click(object sender, ImageClickEventArgs e)
        {
            Nowpage++;
            if (Nowpage <= pagecount)
            {
                lbl_NowPage.Text = Nowpage.ToString();
                WorksBindData(CategoryID, Nowpage);
                PageIndexBindData(pagecount, Nowpage, CategoryID);
            }
        }

    }
}
